
package com.trydofor.id.loader;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.trydofor.aloe.exception.Exceptions;

/**
 * @author : Shi Rongjiu (www.trydofor.com)
 */
public abstract class PureJdbcLoader {
    
    protected static final String SLT_MAKER  = "SELECT MAKER_ID FROM LONG_MAKER WHERE MAKER_NAME=?";
    
    protected final int           makerId;
    protected final int           loaderId;
    protected final DataSource    dataSource;
    
    protected transient int       fetchCount = 100;
    
    /**
     * constructor
     * 
     * @param mk the maker name
     * @param ds the data source
     * @param id the loader id
     * @throws SQLException if can not find maker
     * @throws IllegalArgumentException if id less-equal 0
     */
    public PureJdbcLoader(String mk, DataSource ds, int id) throws SQLException {
        Exceptions.checkNullPointer(mk);
        Exceptions.checkNullPointer(ds);
        Exceptions.checkIllegalArgument(id <= 0);
        dataSource = ds;
        loaderId = id;
        makerId = getMakerIdByName(mk, ds); //
    }
    
    /**
     * constructor
     * 
     * @param mk the maker id
     * @param ds the data source
     * @param id the loader id
     * @throws IllegalArgumentException if id less-equal 0
     */
    public PureJdbcLoader(int mk, DataSource ds, int id) {
        Exceptions.checkNullPointer(ds);
        Exceptions.checkIllegalArgument(id <= 0);
        dataSource = ds;
        loaderId = id;
        makerId = mk;
    }
    
    private int getMakerIdByName(String mk, DataSource ds) throws SQLException {
        PreparedStatement pst = null;
        int id = 0;
        try {
            Connection conn = dataSource.getConnection();
            pst = conn.prepareStatement(SLT_MAKER);
            pst.setString(1, mk);
            ResultSet rs = pst.executeQuery();
            int got = 0;
            for (; rs.next(); got++) {
                id = rs.getInt(1);
            }
            if (got != 1) throw new SQLException("found " + got + " makers, maker=" + mk);
        }
        finally {
            if (pst != null) {
                pst.close();
            }
        }
        return id;
    }
    
    /**
     * @return the fetchCount
     */
    public int getFetchCount() {
        return fetchCount;
    }
    
    /**
     * @param fetchCount the fetchCount to set
     */
    public void setFetchCount(int fetchCount) {
        this.fetchCount = fetchCount;
    }
    
    /**
     * @return the maker Id
     */
    public int getMakerId() {
        return makerId;
    }
    
    /**
     * @return the loader Id
     */
    public int getLoaderId() {
        return loaderId;
    }
    
    /**
     * @return the dataSource
     */
    public DataSource getDataSource() {
        return dataSource;
    }
}
